package com.android.scanner.demo;

import javax.security.auth.PrivateCredentialPermission;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class winActivity extends Activity
{
	private TextView myTextView1 = null;
	private TextView myTextView2 = null;
	private TextView myTextView3 = null;
	
	private EditText myEditText = null;
	
	private Button bCancel = null;
	private Button bConfirm = null;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.windows);
		
		final Intent intent = getIntent();
		//String c_price = intent.getStringExtra("c_price");
		final String userId = intent.getStringExtra("userId");
		final String etString = intent.getStringExtra("etString");
		
		String resultString = getItemInfo(userId, etString);
		String[] strArray = resultString.split("\\|\\|");
		String c_price = strArray[4];

		myTextView2 = (TextView) findViewById(R.id.textView2);
		myTextView2.setText(c_price);
		
		myEditText = (EditText) findViewById(R.id.editText1);
		
		bCancel = (Button) findViewById(R.id.btn_cancel);
		bConfirm = (Button) findViewById(R.id.btn_save);
		
		bCancel.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v)
			{
				winActivity.this.finish();
			}
		});
		
		bConfirm.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v)
			{
//				String tv0 = intent.getStringExtra("tv0"); 
//				String tv1 = intent.getStringExtra("tv1"); 
//				String tv2 = intent.getStringExtra("tv2"); 
//				String tv3 = intent.getStringExtra("tv3"); 

				String new_price = myEditText.getText().toString().trim();
				if(new_price==null)
				{
					myEditText.setError("Plese type a price!");  
					myEditText.requestFocus();  
	                return;
				}
				
				String flag = updatePriceFlag(userId, etString, new_price);
				
				if("1".equals(flag))
				{
					String resultString = getItemInfo(userId, etString);
					String[] strArray = resultString.split("\\|\\|");
					
					Intent intent = new Intent();
					intent.putExtra("tv0", strArray[1]);
					intent.putExtra("tv1", strArray[0]);
					intent.putExtra("tv2", strArray[2]);
					intent.putExtra("tv3", strArray[3]);
					intent.putExtra("new_price", strArray[4]);
					intent.putExtra("tv4", etString);
					intent.putExtra("tv5", userId);
					
					intent.setClass(winActivity.this, ScanDemo.class);
					winActivity.this.startActivity(intent);
				}
				else 
				{
					myEditText.setError("Plese type a correct price!"); 
					myEditText.setText("");
					myEditText.requestFocus();  
	                return;
				}
				
				
				
				
			}
		});
	}
	
	public String getItemInfo(String v_userId, String v_barcode)
	{

		String nameSpace = "http://www.moleq.com/";
		String methodName = "GetItemInfo";
		String endPoint = "http://moleq.com/iphonedemo/iphoneservice.asmx";
		String soapAction = "http://www.moleq.com/GetItemInfo";

		SoapObject rpc = new SoapObject(nameSpace, methodName);
		rpc.addProperty("userID", v_userId);
		rpc.addProperty("barcode", v_barcode);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER10);

		envelope.bodyOut = rpc;
		envelope.dotNet = true;

		envelope.setOutputSoapObject(rpc);
		HttpTransportSE transport = new HttpTransportSE(endPoint);
		transport.debug = true;

		try
		{
			transport.call(soapAction, envelope);
			System.out.println("++++++++++++++++++++++" + transport.requestDump);
			System.out.println("----------------------" + transport.responseDump);
			

		} catch (XmlPullParserException ex)
		{
			System.out.println("++++++++++++++++++++++" + transport.requestDump);
			System.out.println("----------------------" + transport.responseDump);
			ex.getDetail();
			ex.getMessage();
		}

		catch (Exception e)
		{
			e.printStackTrace();

		}

		SoapObject object = (SoapObject) envelope.bodyIn;
		String result = object.getProperty(0).toString();
		System.out.println("%%%%%%%%%%%%%%%%%%" + result);
	
		return result;
	}
	
	public String updatePriceFlag(String v_userId, String v_barcode, String v_newPrice)
	{

		String nameSpace = "http://www.moleq.com/";
		String methodName = "UpdatePrice";
		String endPoint = "http://moleq.com/iphonedemo/iphoneservice.asmx";
		String soapAction = "http://www.moleq.com/UpdatePrice";

		SoapObject rpc = new SoapObject(nameSpace, methodName);
		rpc.addProperty("userID", v_userId);
		rpc.addProperty("barcode", v_barcode);
		rpc.addProperty("newprice",v_newPrice);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER10);

		envelope.bodyOut = rpc;
		envelope.dotNet = true;

		envelope.setOutputSoapObject(rpc);
		HttpTransportSE transport = new HttpTransportSE(endPoint);
		transport.debug = true;

		try
		{
			transport.call(soapAction, envelope);
			System.out.println("++++++++++++++++++++++" + transport.requestDump);
			System.out.println("----------------------" + transport.responseDump);
			

		} catch (XmlPullParserException ex)
		{
			System.out.println("++++++++++++++++++++++" + transport.requestDump);
			System.out.println("----------------------" + transport.responseDump);
			ex.getDetail();
			ex.getMessage();
		}

		catch (Exception e)
		{
			e.printStackTrace();

		}

		SoapObject object = (SoapObject) envelope.bodyIn;
		String result = object.getProperty(0).toString();
		System.out.println("%%%%%%%%%%%%%%%%%%" + result);
	
		return result;
	}

}
